import multiprocessing

TEST_CYCLE = 20 * 60
GETTER_CYCLE = 60 * 60
TEST_ENABLED = True
GETTER_ENABLED = True
API_ENABLE = True

from MySqlClient import TestProxy
from getproxy import Proxy
import time
from flaskDemo import app
from multiprocessing import Process
import logging
import logging.config

log_filename = 'lianjia.log'
logging.basicConfig(level=logging.DEBUG,
                    format='[%(asctime)s] %(levelname)s [%(funcName)s: %(filename)s, %(lineno)d] %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    filemode='a')


def scheduler_tester(cycle=TEST_CYCLE):
    while True:
        print('开始验证ip')
        tester = TestProxy()
        tester.run()
        time.sleep(cycle)


def schedule_getter(cycle=GETTER_CYCLE):
    proxy = Proxy()
    while True:
        logging.info('开始抓取代理')
        proxy.add_proxy()
        time.sleep(cycle)


def schedule_api():
    app.run()


def run():
    logging.info('代理池开始运行')
    logging.info('测试代理')
    tester_process = Process(target=scheduler_tester)
    logging.info('添加代理')
    getter_process = Process(target=schedule_getter)
    logging.info('api运行')
    api_process = Process(target=schedule_api)
    tester_process.start()
    getter_process.start()
    api_process.start()


if __name__ == '__main__':
    run()
